package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

/**
 * 套餐管理
 */
@RestController("backendSetmealController")
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询套餐：{}", queryPageBean);
        return setmealService.pageQuery(queryPageBean);
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result addSetmeal(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds) {
        log.info("新增套餐：{},{}", setmeal,checkgroupIds);
        setmealService.addSetmeal(setmeal,checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        log.info("删除套餐：{}", id);
        setmealService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("根据id查询套餐：{}", id);
        Setmeal setmeal = setmealService.finById(id);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result editSetmeal(@RequestBody Setmeal setmeal,@RequestParam List<Integer> checkGroupIds) {
        log.info("编辑套餐：{},{}", setmeal,checkGroupIds);
        setmealService.editSetmeal(setmeal,checkGroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        log.info("上传图片:{},图片大小:{}kb", imgFile.getName(),imgFile.getSize()/1024);
        String imgUrl = setmealService.upload(imgFile);
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, imgUrl);
    }
}
